﻿using System;
using System.Collections.Generic;

namespace XCSerialPort
{
    public static class EventAggregator
    {
        private static readonly Dictionary<Type, List<Action<object>>> _subscribers = new Dictionary<Type, List<Action<object>>>();

        public static void Subscribe<T>(Action<T> action)
        {
            var type = typeof(T);
            if (!_subscribers.ContainsKey(type))
            {
                _subscribers[type] = new List<Action<object>>();
            }

            // 将 Action<T> 转换为 Action<object>
            Action<object> wrappedAction = obj => action((T)obj);
            _subscribers[type].Add(wrappedAction);
        }

        public static void Publish<T>(T message)
        {
            var type = typeof(T);
            if (_subscribers.ContainsKey(type))
            {
                foreach (var action in _subscribers[type])
                {
                    action(message);
                }
            }
        }
    }

    // 定义日志消息类
    public class LogMessage
    {
        public string Message { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.Now;
    }
}
